<?php
/**
 *  
 *
  */
namespace Org\Util;
class Rxyft {

    
     public function xyft_balance() {
		$day=date("Y-m-d");
		 $day_time=strtotime($day);
		 $insert_data=array();
		 $lm=array();
 				  $n_time=time(); 
 				    $condition['c_id']=5;	
					//$condition['add_time'] = array(array('gt',$day_time)) ; 
 					$max_c_no=M('lottery_results')->where($condition)->max('c_no'); //获取当天最大期数
					 
					$url='http://a.apiplus.net/newly.do?token=5ee5aca63461f954&code=mlaft&rows=1&format=json'; //接口地址
					
					$new_result=get_http($url);
					$result=json_decode($new_result,true);
					 //print_r($max_c_no);
					// print_r($result['data'][0]['expect']);exit;
				 if($max_c_no!=$result['data'][0]['expect']){
				 	//  print_r($result['data'][0]['expect']);exit;
 					$num= explode(",",$result['data'][0]['opencode']);
					foreach($num as $k => $v){
						$num[$k]=preg_replace('/^0+/','',$v);   //去除左边的0
						}
					//	print_r($n_num);exit;
					$insert_data['c_no']=$result['data'][0]['expect'];
					$insert_data['c_id']=5;
					$insert_data['c_name']='幸运飞艇';
					$insert_data['add_time']=$result['data'][0]['opentimestamp'];
					$insert_data['lottery_result']=implode(",",$num);
					$insert_data['status']=0;
					S('xyft_'.$result['data'][0]['expect'],implode(",",$num));  //存入缓存  方便计算两面长龙算法
					//$sum=array_sum($num);
					$sum=$num[0]+$num[1];
					$lm[0]=$sum;
					$lm[1]=$this->gy_ds($sum);
		            $lm[2]=$this->gy_dx($sum);
		            $o_lm=$this->lht($num);
					foreach($o_lm as $k=>$v){
						$lm[$k]=$v;
						}
						 
				    $n_lm=implode(",",$lm);
					 
					$insert_data['lm']=$n_lm;
				 // print_r($insert_data);exit;
					if($max_c_no+1==$result['data'][0]['expect']){  //计算两面长龙结果
					  
					  $bdx=$this->bdx($num); //1-10大小
					  $bds=$this->bds($num); //1-10单双 
 					   $flag=1;
 					   $flag_gyds=0;
					   $flag_gydx=0;
					     
					    for($i=1;$i<=10;$i++){
						  $strds='flag_bds'.$i;
					      $$strds=0;
						   
						  $strdx='flag_bdx'.$i;
					      $$strdx=0;
					   }
					   $k=1;
					   do{
						   
						   $old_lottery=$result['data'][0]['expect']-$k;
						   //S('xyft_'.$old_lottery,NULL);
						   $xyft_result=S('xyft_'.$old_lottery);
			 
							if(empty($xyft_result))
							{
							$condition1['c_id']=5;
							$condition1['c_no']=$old_lottery;
							$xyft_result=M('lottery_results')->where($condition1)->getField('lottery_result');
							if($xyft_result){
							S('xyft_'.$old_lottery,$xyft_result);
							}else{
								$flag=0;
								}
							}	
						  
 						   $num1[$k-1]= explode(",",$xyft_result);
						    
						   $sum1[$k-1]=$num1[$k-1][0]+$num1[$k-1][1];
						   
						     //计算冠亚和单双两面长龙
						   $ds1=$this->gy_ds($sum1[$k-1]);
						   if($flag_gyds==0){
							   
							  if($ds1==$lm[1]){
							   $b=$k+1;
							   //ZDS=D,2,总和单
								 if($ds1=='双'){
								 $gy_ds='GDS=S,'.$b.',冠亚双';
								 }else{
								 $gy_ds='GDS=D,'.$b.',冠亚单';
									 }
							   }else{
							   $flag_gyds=1;	 
								   }
						   }
						   //计算冠亚和大小两面长龙
						   $dx1=$this->gy_dx($sum1[$k-1]);
						   if($flag_gydx==0){
							   
							  if($dx1==$lm[2]){
							   $b=$k+1;
							   //ZDS=D,2,总和单
								 if($dx1=='小'){
								 $gy_dx='GDX=X,'.$b.',冠亚小';
								 }else{
								 $gy_dx='GDX=D,'.$b.',冠亚大';
									 }
							   }else{
							   $flag_gydx=1;	 
								   }
						   } 
						   
						 $bds1=$this->bds($num1[$k-1]);
						 for($i=1;$i<=10;$i++){
						  $str='flag_bds'.$i;
						  $str1='b_ds'.$i;	
						  switch ($i)
							{
							 case 1:$gread='冠军';
							 break;
							 case 2:$gread='亚军';
							 break;
							 case 3:$gread='第三名';
							 break;
							 case 4:$gread='第四名';
							 break;
							 case 5:$gread='第五名';
							 break;
							 case 6:$gread='第六名';
							 break;
							 case 7:$gread='第七名';
							 break;
							 case 8:$gread='第八名';
							 break;
							 case 9:$gread='第九名';
							 break;
							 case 10:$gread='第十名';
							 break;
							  default:
 									break;
								
							  }
						   if($$str==0){
							   $ab=$i-1;
							  if($bds1[$ab]==$bds[$ab]){
								 
							   $b=$k+1;
								 if($bds1[$ab]=='双'){
								 $$str1='DS'.$i.'=S,'.$b.','.$gread.'-双';
								 }else{
								 $$str1='DS'.$i.'=D,'.$b.','.$gread.'-单';
									 }
							   }else{
							   $$str=1;	 
								   }
						   }
						  }
						   
						  $bdx1=$this->bdx($num1[$k-1]);
						
						 for($i=1;$i<=10;$i++){
						  $str2='flag_bdx'.$i;
						  $str3='b_dx'.$i;	
						  switch ($i)
							{
							 case 1:$gread='冠军';
							 break;
							 case 2:$gread='亚军';
							 break;
							 case 3:$gread='第三名';
							 break;
							 case 4:$gread='第四名';
							 break;
							 case 5:$gread='第五名';
							 break;
							 case 6:$gread='第六名';
							 break;
							 case 7:$gread='第七名';
							 break;
							 case 8:$gread='第八名';
							 break;
							 case 9:$gread='第九名';
							 break;
							 case 10:$gread='第十名';
							 break;
							  default:
 									break;
								
							  }
						   if($$str2==0){
							   $ab=$i-1;
							  if($bdx1[$ab]==$bdx[$ab]){
								 
							   $b=$k+1;
								 if($bdx1[$ab]=='小'){
								 $$str3='DX'.$i.'=X,'.$b.','.$gread.'-小';
								 }else{
								 $$str3='DX'.$i.'=D,'.$b.','.$gread.'-大';
									 }
							   }else{
							   $$str2=1;	 
								   }
						   }
						  } 
						   
						   
						   $k++;
					      $sss1='flag_bds1';
						  $sss2='flag_bds2';
						  $sss3='flag_bds3';
						  $sss4='flag_bds4';
						  $sss5='flag_bds5';
						  $sss6='flag_bds6';
						  $sss7='flag_bds7';
						  $sss8='flag_bds8';
						  $sss9='flag_bds9';
						  $sss10='flag_bds10';
						  
						  $xxx1='flag_bdx1';
						  $xxx2='flag_bdx2';
						  $xxx3='flag_bdx3';
						  $xxx4='flag_bdx4';
						  $xxx5='flag_bdx5';
						  $xxx6='flag_bdx6';
						  $xxx7='flag_bdx7';
						  $xxx8='flag_bdx8';
						  $xxx9='flag_bdx9';
						  $xxx10='flag_bdx10';
						  if($flag_gyds==1&&$flag_gydx==1&&$$xxx1==1&&$$xxx2==1&&$$xxx3==1&&$$xxx4==1&&$$xxx5==1&&$$xxx6==1&&$$xxx7==1&&$$xxx8==1&&$$xxx9==1&&$$xxx10==1&&$$sss1==1&&$$sss2==1&&$$sss3==1&&$$sss4==1&&$$sss5==1&&$$sss6==1&&$$sss7==1&&$$sss8==1&&$$sss9==1&&$$sss10==1||$k>10){
							// if($k<11){
							   $flag=0;
							   }
							// $flag=0;  
						   }while($flag==1);
					  
					  
					   if($gy_ds){	 
					   $s[]=$gy_ds;	
					   }
					   if($gy_dx){	 
					   $s[]=$gy_dx;	
					   } 
					   for($i=1;$i<=10;$i++){
		                 $strx='b_ds'.$i;
			             if($$strx){	 
					     $s[]=$$strx;	
					      }
						  
						  $strbdx='b_dx'.$i;
			              if($$strbdx){	 
					       $s[]=$$strbdx;	
					      }
					   }
					  // print_r($s);exit;
					   $n_s=implode(";",$s);	
						// print_r($n_s);exit;
					  $insert_data['detail']=$n_s;	
						}
						
					$insert_result=M('lottery_results')->data($insert_data)->add();	
				 	if(!$insert_result){
 						 file_put_contents("test.txt", date("Y-m-d H:i:s") . "获取幸运飞艇结果存入失败！" . "\r\n<br>", FILE_APPEND);  	
							}else{
						$xyft_result = new \Org\Util\Jsxyft();  //幸运飞艇
        $xyft_result->xyft_balance();
					}
						
					}
					 
		return; 		
     }
	
 public function gy_ds($sum){  //冠亚单双
      if($sum%2==0){
		$ds='双';
			}else{
		$ds='单';	 
		}
		return $ds;
 }
 public function gy_dx($sum){  //冠亚大小
      if($sum>11){
		   $dx='大';	 
			 }else{
			 $dx='小';	 
				 }
		return $dx;
 }
 public function lht($num){  //龙虎
      if($num[0]>$num[9]){
			$lht[3]='龙';	 
			 }else{
			$lht[3]='虎';	  
				 } 
	 if($num[1]>$num[8]){
			$lht[4]='龙';	 
			 }else{
			$lht[4]='虎';	  
				 }
	if($num[2]>$num[7]){
			$lht[5]='龙';	 
			 }else{
			$lht[5]='虎';	  
				 }	
	if($num[3]>$num[6]){
			$lht[6]='龙';	 
			 }else{
			$lht[6]='虎';	  
				 }	
	if($num[4]>$num[5]){
			$lht[7]='龙';	 
			 }else{
			$lht[7]='虎';	  
				 }
    			 			 		 		  			 
	return $lht;
 }
 
 public function bdx($num){  //1-10大小
	 $bdx=array();
	 foreach($num as $k=>$v){
		 if($v>5){
		   $bdx[$k]='大';	 
			 }else{
			$bdx[$k]='小';	 
				 }
 		 }
	return $bdx;	 
  }
 public function bds($num){  //1-10单双
	 $bds=array();
	 foreach($num as $k=>$v){
		 if($v%2==0){
		   $bds[$k]='双';	 
			 }else{
			$bds[$k]='单';	 
				 }
 		 }
	return $bds;	 
  }
	 
}